use t::MYTEST 'no_plan';

# PERL5LIB=.:$PERL5LIB prove t/core/log.t -v

# repeat_each。它其实是一个 perl 函数，默认情况下是 repeat_each(1)，表示测试案例只运行一次
repeat_each(1);
no_long_string();
no_root_location();

run_tests();


__DATA__

=== TEST 1: error log
--- config

    location /t {
        content_by_lua_block {
            local core = require("service.core")
            core.log.error("error log")
            core.log.warn("warn log")
            core.log.notice("notice log")
            core.log.info("info log")
            ngx.say("done")
        }
    }
--- log_level: error
--- request
GET /t
--- error_log
error log
--- no_error_log
warn log
notice log
info log

=== TEST 2: warn log
--- config
    location /t {
        content_by_lua_block {
            local core = require("service.core")
            core.log.error("error log")
            core.log.warn("warn log")
            core.log.notice("notice log")
            core.log.info("info log")
            core.log.debug("debug log")
            ngx.say("done")
        }
    }
--- log_level: warn
--- request
GET /t
--- error_log
error log
warn log
--- no_error_log
notice log
info log
debug log

=== TEST 3:notice log
--- config
    location /t {
        content_by_lua_block {
            local core = require("service.core")
            core.log.error("error log")
            core.log.warn("warn log")
            core.log.notice("notice log")
            core.log.info("info log")
            core.log.debug("debug log")
            ngx.say("done")
        }
    }
--- log_level: notice
--- request
GET /t
--- error_log
error log
warn log
notice log
--- no_error_log
info log
debug log

=== Test 4: info log
--- config
    location /t {
        content_by_lua_block {
            local core = require("service.core")
            core.log.error("error log")
            core.log.warn("warn log")
            core.log.notice("notice log")
            core.log.info("info log")
            core.log.debug("debug log")
            ngx.say("done")
        }
    }
--- log_level: info
--- request
GET /t
--- error_log
error log
warn log
notice log
info log
--- no_error_log
debug log


=== TEST 5: debug log
--- config
    location /t {
        content_by_lua_block {
            local core = require("service.core")
            core.log.error("error log")
            core.log.warn("warn log")
            core.log.notice("notice log")
            core.log.info("info log")
            core.log.debug("debug log")
            ngx.say("done")
        }
    }
--- log_level: debug
--- request
GET /t
--- error_log
error log
warn log
notice log
info log
debug log

=== TEST 6: print error log with prefix
--- config
    location /t {
        content_by_lua_block {
            local log_prefix = require("service.core").log.new("prefix: ")
            log_prefix.error("error log")
            log_prefix.warn("warn log")
            log_prefix.notice("notice log")
            log_prefix.info("info log")
            log_prefix.debug("debug info")
            ngx.say("done")
        }
    }
--- log_level: error
--- request
GET /t
--- error_log eval
qr/[error].+prefix: error log/
--- no_error_log
[qr/[warn].+warn log/,qr/[notice].+notice log/,qr/[info].+info log/,qr/[debug].+debug log/]

=== Test 7: print both prefixed error logs and normal logs
--- config
    location /t {
        content_by_lua_block {
            local core = require("service.core")
            local log_prefix = core.log.new("prefix: ")
            core.log.error("raw error log")
            core.log.warn("raw warn log")
            core.log.notice("raw notice log")
            core.log.info("raw info log")

            log_prefix.error("error log")
            log_prefix.warn("warn log")
            log_prefix.notice("notice log")
            log_prefix.info("info log")
            ngx.say("done")
        }
    }
--- log_level: error
--- request
GET /t
# qr// 是perl中适用于模式匹配和相关事件的类似引号的运算符之一
--- error_log eval
[qr/[error].+raw error log/,qr/[error].+prefix: error log/]
--- no_error_log
[qr/[warn].+warn log/,qr/[notice].+notice log/,qr/[info].+info log/]
